<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/*
 * author	:	Tran Van Thanh
 * email	:	thanhtran@vietnambiz.com
 * date		:	01/09/2011
 */

require_once 'client'.EXT;

class Questions extends Client {

	function __construct()
	{
		parent::__construct();		
        $this->load->library(array("form_validation","Message","pagination"));
		$this->load->helper(array('custom_string','custom_link'));
		$this->load->model(array('questions_model','home_model'));	

		$this->data['support'] 	= $this->home_model->get_support();
		
		$this->data['products_best_sales']	= $this->home_model->get_all_products('best_sales',5);
		
		$this->data['questions_usually']	= $this->questions_model->get_questions_usually();
	}
	
	function index($page = 1)
	{		
		$limit		=	8;		
		$offset     = 	($page-1)*$limit;
				
		$result		=	$this->questions_model->get_all_questions($limit,$offset);

		$num_rows   =	$result['num_rows'];
		$rows		=	$result['rows'];
		
		$total		=	ceil($num_rows/$limit);			
		
		$url		=	base_url().'tu-van-hoi-dap';		
			
		$conf = array(
			'cur_page'			=>	$page,
			'base_url'			=>	$url,
			'total_rows'		=>	$total,
			'cur_page'			=>	$page,			
			'cur_class'			=>	'currentpage',
			'prev_class'		=>	'prevnext',
			'next_class'		=>	'prevnext',
			'next_link'			=>	'Tiếp theo',
			'prev_link'			=>	'Trang trước',
			'show_total'		=>	'no',
			'show_first_last'	=>	'no'
		);
		
		$this->pagination->initialize($conf);
		$this->data['pagnav'] 		= $this->pagination->non_ajax();
		$this->data['page']			= $page;
		$this->data['num_rows'] 	= $num_rows;
		$this->data['total_page'] 	= $total;
		
		$this->data['all_questions']		= $rows;

		//meta tag
	    $this->data['title'] 			= get_title('[TITLE]',"Tư vấn hỏi đáp");
	    $this->data['description'] 		= get_description('[DESCRIPTION]',"Tư vấn hỏi đáp");
		$this->data['keywords'] 		= get_keywords('[KEYWORDS]',"Tư vấn hỏi đáp");	    
		//end meta tag
		
		$this->data['tpl_file']		=	'questions/index';
		$this->load->view('layout/default',$this->data);
	}
	
	function send_questions()
	{
		if ($this->input->server('REQUEST_METHOD') === 'POST')
        {                     	  	
        	$this->form_validation->set_message('required', '%s');
            $this->form_validation->set_message('valid_email', '%s');
            $this->form_validation->set_rules("txtName","Họ tên","required");            
            $this->form_validation->set_rules("txtEmail","Email","required|valid_email");
            $this->form_validation->set_rules("txtPhone","Điện thoại","required");              
            $this->form_validation->set_rules("txtTitle","Tiêu đề","required");
            $this->form_validation->set_rules("txtContent","Nội dung","required");
                                  
            if($this->form_validation->run() === TRUE)
            {            	
            	 $fullname			=	$this->input->post('txtName');                 
            	 $email          	=	$this->input->post('txtEmail');
                 $phone				=	$this->input->post('txtPhone');
                 $title				=	$this->input->post('txtTitle');
                 $content           = 	$this->input->post('txtContent');
                 $id_customer		=  	0;
                 
                 if(strtolower($this->input->post('val')) == strtolower($this->session->userdata('security_code')))
				 {				 	
	                 /* Insert into tables customers*/
	                 if($this->questions_model->check_email_customers($email)==FALSE)
	                 {
		                 $data_customers = array(
		                 	'fullname'		=> strip_tags($fullname),
		                 	'email'			=> strip_tags($email),
		                 	'address'		=> "",
		                 	'telephone'		=> strip_tags($phone),
		                 	'date_create'	=> date('Y-m-d H:i:s'),
		                 	'active'		=> 'yes'
		                 );
		                 $this->db->insert('customers', $data_customers);
			             $id_customer = $this->db->insert_id();
	                 }
	                 else
	                 {
	                 	$customer_info = $this->questions_model->get_customers_info_by_email($email);
		                $id_customer   = $customer_info->id;  
	                 }
	                 
	                 /* Insert into tables questions*/
	                 $data_questions = array(
	                 	'title'			=> strip_tags($title),
	                 	'title_link'	=> ascii_link($title,'-'),
	                 	'contents'		=> strip_tags($content),
	                 	'id_customer'	=> $id_customer,
	                 	'date_create'	=> date('Y-m-d H:i:s'),
	                 	'usually'		=> 'no',
	                 	'status'		=> 'yes'
	                 );
	                 $add  	= $this->db->insert('questions', $data_questions);	                       
					 if($add == TRUE)
	                 {
	                 	$this->message->add("success",'<li class=success><p>Bạn đã gửi câu hỏi thành công! Xin cảm ơn!<META HTTP-EQUIV=refresh CONTENT=4;URL="'.base_url().'tu-van-hoi-dap'.'"></p></li>');
	                 }	                
				 }
				 else
				 {
				 	$this->message->add("error",'<li class=error><p>Mã xác nhận không đúng!</p></li>');
				 }                   
            }           
        }
		$this->data['tpl_file']		=	'questions/send_questions';
		$this->load->view('layout/default',$this->data);
	}
	
	function captcha()
  	{
    	$this->load->helper('date');
      	$this->load->library('captcha_image');
      	$config=array(
        	'height'  		=>  '248',
        	'width'   		=>  '90',
        	'num_string'    => 	6,
        	'x'    			=>  10,
        	'text_color' 	=> 	array(0,0,255)      		      		   
      	);
	    $this->captcha_image->initialize($config);
      	$this->data['img']  = $this->captcha_image->output();
      	$this->load->view('captcha',$this->data);      
  	} 

  	function detail($id_questions = null,$title_link = null)
  	{  		  		
  		$questions_info						= $this->questions_model->get_questions_detail($id_questions);  		 					
  		$this->data['questions_info'] 		= $questions_info;
  		
  		//meta tag
	    $this->data['title'] 			= get_title('[TITLE]',$questions_info[0]->title);
	    $this->data['description'] 		= get_description('[DESCRIPTION]',$questions_info[0]->title);
		$this->data['keywords'] 		= get_keywords('[KEYWORDS]',$questions_info[0]->title);	    
		//end meta tag
		
  		$this->data['tpl_file']		=	'questions/detail';
		$this->load->view('layout/default',$this->data);
  	}
  	
  	function search_questions($keyword = null,$page = 1)
  	{  		
  		$limit		=	8;		
		$offset     = 	($page-1)*$limit;
		
		$keyword 	= str_replace('+',' ',$keyword);
		$keyword 	= str_replace('_',' ',$keyword);
		$keyword 	= str_replace('-',' ',$keyword);
		$keyword 	= preg_replace('/[\:\!\?\&\<\>\#\'\"]+/is','',$keyword);
		
		$result		=	$this->questions_model->search_questions($keyword,$limit,$offset);

		$num_rows   =	$result['num_rows'];
		$rows		=	$result['rows'];
		
		$total		=	ceil($num_rows/$limit);			
		
		$url		=	search_link($keyword);		
			
		$conf = array(			
			'base_url'			=>	$url,
			'per_page'			=>	$limit,
			'cur_page'			=>	$page,
			'total_rows'		=>	$total,
			'num_space'			=>	'',
			'next_link'			=> 	'Tiếp theo',
			'prev_link'			=>	'Trang trước',
			'last_link'			=>  'Trang cuối',
			'first_link'		=> 	'Trang đầu',
			'show_first_last'	=> 'no'
        );
	
		$this->pagination->initialize($conf);
		$this->data['pagnav']       		= $this->pagination->display();
		$this->data['page']         		= $page;
		$this->data['num_rows']     		= $num_rows;
		$this->data['total_page']   		= $total;
		
		$this->data['all_questions']		= $rows;

		//meta tag
	    $this->data['title'] 			= get_title('[TITLE]',"Tìm kiếm");
	    $this->data['description'] 		= get_description('[DESCRIPTION]',"Tìm kiếm");
		$this->data['keywords'] 		= get_keywords('[KEYWORDS]',"Tìm kiếm");	    
		//end meta tag
		
		$this->data['tpl_file']		=	'questions/index';
		$this->load->view('layout/default',$this->data);	
  	}
}